<?php
/**
 * @ClassName: Membership
 * @Description: 会员类型
 * @author: gydtrade
 * @Date: 2024-09-09 22:44
 * @Version: V1.0.0
 */

namespace app\admin\controller\paidmember;

use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Membership extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\member\MemberShip();
        $this->pageTitle = '会员类型';
        $this->enableField = 'status';
        $this->pagesize = 10;

        $this->selectTextField = '{title}';
        $this->selectFields = 'id,title';
        $this->selectSearch = 'title';
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;
        $search->text('title', '会员名称');
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if (!empty($searchData['title'])) {
            $where[] = ['title', 'like', '%' . $searchData['title'] . '%'];
        }
        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->show('title', '会员名称');
        $table->show('vip_day', '有限期（天）');
        $table->show('price', '原价');
        $table->show('pre_price', '优惠价');
        $table->show('sort', '排序');
        $table->switchBtn('status', '是否开启')->autoPost();

        $table->fields('create_times', '添加/修改时间')->with(
            $table->show('create_time', '添加时间'),
            $table->show('update_time', '修改时间')
        )->getWrapper()->addStyle('width:160px');

        $table->sortable('id, sort, create_time');

        $table->getToolbar()
            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
            ->btnDelete()
            ->btnView();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;
        $form->text('title', '会员名称')->required();
        $form->number('vip_day', '有限期（天）')->required();
        $form->number('price', '原价')->required();
        $form->number('pre_price', '优惠价')->required();
        $form->number('sort', '排序')->required();
        $form->switchBtn('status', '是否开启')->default(1)->required();

        if ($isEdit) {
            $form->show('create_time', '添加时间');
            $form->show('update_time', '更新时间');
        }
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->only([
            'title',
            'vip_day',
            'price',
            'pre_price',
            'sort',
            'status'
        ], 'post');

        $result = $this->validate($data, [
            'title|会员名称' => 'require',
            'vip_day|有限期' => 'require|number',
            'price|原价' => 'require|number',
            'pre_price|优惠价' => 'require|number',
            'sort|排序' => 'require|number',
        ]);

        if (!$result) {
            $this->error($result);
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }

}